package com.xqb.listen.ui.widegts

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.xqb.listen.R
import com.xqb.listen.ui.theme.White

/**
 * @author Ling
 * @date :2025/1/23 10:34
 */

@Composable
fun PageToolbar(
    modifier: Modifier = Modifier,
    title : String? = null,
    titleAlignment: Alignment = Alignment.Center,
    rightIcon : Int? = null,
    onIconClick : (isExit : Boolean) -> Unit
){
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ){
        Icon(
            painter = painterResource(id = R.drawable.ic_exit),
            contentDescription = null ,
            tint = White,
            modifier = Modifier
                .clip(CircleShape)
                .clickable {
                onIconClick(true)
            }
        )

        if(!title.isNullOrEmpty()){
            Text(
                text = title,
                color = Color.White,
                fontSize = 16.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.weight(1f),
                textAlign = if(titleAlignment == Alignment.Center) TextAlign.Center else TextAlign.Start
            )
        }
        if(rightIcon != null){
            Icon(
                painter = painterResource(id = rightIcon),
                contentDescription = null,
                modifier = Modifier
                    .clip(CircleShape)
                    .clickable {
                    onIconClick(false)
                }
            )
        }else{
            Spacer(modifier = modifier.size(48.dp))
        }
    }
}

@Composable
@Preview(showBackground = true , showSystemUi = true)
fun PageToolbarPreview(){
    PageToolbar(title = "测试"){}
}